***************************************************
/* This do-file replicates Figure 2 of the paper */
***************************************************

set more off 

cd "" /*input directory here with cleaned data south_africa_GHS using the ad hoc GHS do-file */

use south_africa_GHS.dta, clear 

bys household_id year: egen grant_h=max(child_grant)  /* variable at the household level equal to 1 if anybody in the household receives the grant */ 


gen cohort= year-age /* No Information for cohort, given that GHS takes place in July, year - age gives a cohort born from july of t-1 to june of t. See paper for detailed explanation */  

drop if  race==3 | race==4 /* Exclude population groups that do not receive CSG to a significant extent, see text for explanation */ 


*********************************************
* CSG take- up by youngest cohort and year ** 
*********************************************

* Merge Child-Level information on grant with recipient mothers * 

preserve 

keep household_id cohort mother_id child_grant age year

rename mother_id id 
rename cohort cohort_child 
rename child_grant grant_child
rename age age_child 

sort household_id id  

save children_GHS.dta, replace

restore 

merge 1:m household_id year id using children_GHS.dta 

erase children_GHS.dta

keep if _merge==3 /* this only keeps mother with children observed in the household */ 

keep if sex==2 /* Excludes a few mistmatched observations (matched with man, <1%) */ 
drop if age<age_child+15 /* Excludes if age of the child is too young wrt to mother (1%) */ 


* Keep only the youngest child for each mother * 

egen id_mother=group(household_id id year)

drop child_grant 

bys id_mother: egen youngest_child_cohort=max(cohort_child) /* for each individual mother, identify the youngest child */ 

keep if cohort_child==youngest_child_cohort /* keeps only observations for the youngest.  */ 

bys id_mother: egen child_grant_mother=max(grant_child) /* This defines grant receipt at the mother level. Nothing changes if for the mother has only one 
youngest child. If the mother has two youngest children of the same cohort (roughly 3% of the sample), it creates a variable equal to 1 if any of the two children receives the child support grant  */


keep if cohort_child>=1991 & cohort_child<=1995 /* Relevant cohorts */

duplicates drop id_mother, force /*keep only one observation per mother */ 


* Collapse weighted data at the child cohort level *

collapse child_grant_mother grant_h  [w=weight], by(cohort_child year)


*********************************
** Correction for 2002 **********
*********************************

* NB: in 2002 CSG is coded only at the household level and not linked to the child. To create a comparable time series with the remaning years, the 2002 share of households receiving the CSG is reweighted by the ratio of the share calculated at the individual level and the share calculated at the household level in the remaning years, where both measures can be obtained*

egen nominator=mean(child_grant_mother)
egen denominator=mean(grant_h) 
gen ratio=nominator/denominator /* receipt at the individual level is on average 1/3 than the one at the household level over 2003-2011 */ 

replace child_grant_mother=child_grant_mother*ratio if year==2002 


**************************
***** Figure 2 ***********
**************************

twoway (line child_grant_mother year if cohort_child==1991, sort lcolor(black) lpattern(dot)  ) || (line child_grant_mother year if cohort_child==1992, sort lcolor(black)   ) || (line child_grant_mother year if cohort_child==1993, sort lcolor(black) lpattern(shortdash) ) || (line child_grant_mother year if cohort_child==1994, sort lcolor(black) lpattern(dash) ) || (line child_grant_mother year if cohort_child==1995, sort lcolor(black) lpattern(longdash)) ///
|| (scatter child_grant_mother year if cohort_child==1991, sort msize(small) mcolor(black)  ) || (scatter child_grant_mother year if cohort_child==1992, sort msize(small) mcolor(black) ) || (scatter child_grant_mother year if cohort_child==1993, sort msize(small) mcolor(black) ) || (scatter child_grant_mother year if cohort_child==1994, sort msize(small) mcolor(black) ) || (scatter child_grant_mother year if cohort_child==1995, sort msize(small) mcolor(black)) /// 
||,  graphregion( color(white) ) plotregion(  fcolor(white) ) ytitle("% of Mothers Receiving CSG") xtitle("Year")  xline(2002, lcolor(black) lpattern(dash)) xline(2006.75, lcolor(black) lpattern(dash)) xline(2011, lcolor(black) lpattern(dash)) /// 
||, text( 0.47 2002.75 "{&lArr}First Census" , size(small )  tstyle(subheading )  just(left)   ) ///
||, text( 0.47 2005.75 "Community Survey{&rArr}" , size(small )  tstyle(subheading )  just(left) ) ///
||, text( 0.47 2010.15 "Second Census{&rArr}" , size(small )  tstyle(subheading )  just(left)   ) ///
||, ylabel(0 (0.1) 0.5) xlabel (2002 (2) 2011) ///
||, legend(rows(2) order(1 2 3 4 5) label(1 "Cohort 1991") label(2 "Cohort 1992") label (3 "Cohort 1993") label(4 "Cohort 1994") label(5 "Cohort 1995"))

